const baseURL: string = process.env.VUE_APP_BASE_DEV
type Method = 'GET' | 'POST' | 'PUT' | "DELETE" | 'OPTIONS'

interface RequestParams extends UniApp.RequestOptions { 
  method?: Method
  url: string
}


export default {
  request(options: RequestParams): Promise<any> {
    return new Promise((resolve, reject) => {
      let { method = 'GET', url, data, ...rest } = options
      url = baseURL + url
      uni.request({
        method,
        url,
        ...(data ? { data } : {}),
        ...rest,
        success: res => resolve(res.data),
        fail: err => reject(err)
      })
    })
  },
  get(url: string, data?: any): Promise<any> {
    return this.request({
      method: 'GET',
      url,
      data,
    })
  },
  post(url: string, data?: any): Promise<any> {
    return this.request({
      method: 'POST',
      url,
      data,
    })
  },
  put(url: string, data?: any): Promise<any> {
    return this.request({
      method: 'PUT',
      url,
      data,
    })
  },
  del(url: string, data?: any): Promise<any> {
    return this.request({
      method: 'DELETE',
      url,
      data,
    })
  },
}